import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { observable, Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class BankTransferService {

  constructor(
    private http: HttpClient
  ) { }

  pageData: any = null;
  pageType = 2;
  bankListData: any;

  // 1. 银行划扣 列表
  getBankTransferList(pageNumber = 1, pageSize = 5): Observable<any>{
    const url = '/api/gsxmcp/cg/um/v1.0/balance/page';
    let args = {
      pageNumber,
      pageSize
    }

    let params = new HttpParams({fromObject: args});
    return this.http.get(url, { params });
  }
  getBankTransferChangeList(pageNumber = 1, pageSize = 5): Observable<any>{
    const url = '/api/gsxmcp/cg/um/v1.0/balance/applied';
    let args = {
      pageNumber,
      pageSize
    }

    let params = new HttpParams({fromObject: args});
    return this.http.get(url, { params });
  }

  // 2. 新增 银行划扣
  addBankTransfer(params: any): Observable<any>{
    const url = '/api/gsxmcp/cg/um/v1.0/balance/add';

    return this.http.post(url, params);
  }

  // 3. 修改 银行划扣
  updateBankTransfer(params: any){
    const url = '/api/gsxmcp/cg/um/v1.0/balance/update';

    return this.http.post(url, params);
  }

  // 4. 删除 银行划扣
  deleteBankTransfer(usageCode: any): Observable<any>{
    let url = `/api/gsxmcp/cg/um/v1.0/balance/delete/${usageCode}`;

    return this.http.get(url);
  }

  // 5. 查询 银行划扣
  // getBankList(type, name): Observable<any>{
  //   let url = '/api/gsxmcp/cg/um/v1.0/bank/infos';
  //   let args = {
  //     type,
  //     name
  //   }
  //   console.log(args);
  //   if(type === '' || name === '' || name === null){
  //     return new Observable((observer) => {
  //       observer.next(0)
  //     })
  //   }

  //   let params = new HttpParams({fromString: queryString.stringify(args)});
  //   this.bankListData=this.http.get(url, { params });
  //   return this.http.get(url, { params });
  // }
  getBankList(type: any, name: any): Observable<any>{
    let url = ''
    if(type){
      url = `/api/gsxmcp/cg/um/v1.0/bank/infos?name=${name}&type=${type}`
    }else{
      url = `/api/gsxmcp/cg/um/v1.0/bank/infos?name=${name}`;
    }
    
    if(name === '' || name === null){
      return new Observable((observer) => {
        observer.next(0)
      })
    }

    this.bankListData=this.http.get(url);
    return this.http.get(url);
  }

  // 6. 查看协议
  getPDF(formData: any): Observable<any>{
    const url = '/api/gsxmcp/pb/po/v1.0/getPDF';

    return this.http.post(url, formData, {responseType: 'blob'});
  }
  // 7. 保存协议
  savePDF(formData: any): Observable<any>{
    const url = '/api/gsxmcp/pb/po/v1.0/pdf';

    return this.http.post(url, formData);
  }

  // 查询代码集
  getCodeItems(type: any): Observable<any>{
    const api = `/api/gsxmcp/bd/cm/v1.0/codeItems/${type}`;
    return this.http.get(api);
  }

}

export type BankTransferList = {
  id: string,
  bankId: string,
  usageCode: string,
  businessType: string,
  balanceName: string,
  bankName: string,
  account: string,
  address: string,
  pdfId: string
}
